﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils;

namespace UI.Desktop.Administradores
{
    public partial class Register : Form
    {
        private Entities.Administrador _current_instance = new Entities.Administrador();
        private Logic.Administradores _hnd_administradores = new Logic.Administradores();
        private bool _need_refresh = false;
        private CommonControls.FormMode _form_mode = CommonControls.FormMode.New;

        private Entities.Administrador CurrentInstance
        {
            get { return _current_instance; }
            set { _current_instance = value; }
        }
        private Logic.Administradores HndAdministradores
        {
            get { return _hnd_administradores; }
        }
        private bool NeedRefresh
        {
            get { return _need_refresh; }
            set { _need_refresh = value; }
        }
        private CommonControls.FormMode FormMode
        {
            get { return _form_mode; }
            set { _form_mode = value; }
        }

        public Register()
        {
            InitializeComponent();
            InitializeControl();
            Text += "Agregar";
        }

        public Register(Entities.Administrador instance)
        {
            InitializeComponent();
            InitializeControl();
            FormMode = CommonControls.FormMode.Modify;
            Text += "Editar";
            CurrentInstance = instance;
            CurrentInstance.State = Commons.EntityState.Modified;
            txtNombre.Text = CurrentInstance.Nombre;
            txtApellido.Text = CurrentInstance.Apellido;
            txtDireccion.Text = CurrentInstance.Direccion;
            txtTelefono.Text = CurrentInstance.Telefono;
            txtEmail.Text = CurrentInstance.Email;
            dtpFechaNacimiento.Value = CurrentInstance.FechaNacimiento;
        }

        private void InitializeControl()
        {
            dtpFechaNacimiento.Value = dtpFechaNacimiento.MaxDate = DateTime.Today;
        }

        private string Save()
        {
            CurrentInstance.Nombre = txtNombre.Text;
            CurrentInstance.Apellido = txtApellido.Text;
            CurrentInstance.Direccion = txtDireccion.Text;
            CurrentInstance.Telefono = txtTelefono.Text;
            CurrentInstance.Email = txtEmail.Text;
            CurrentInstance.FechaNacimiento = dtpFechaNacimiento.Value;

            return HndAdministradores.Save(CurrentInstance);
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            string r = Save();

            if (!r.IsEmptyString()) CommonControls.ErrorBox(Text, r);
            else
            {
                NeedRefresh = true;
                if (FormMode == CommonControls.FormMode.New)
                {
                    if (CommonControls.AskBox(Text, "Nuevo administrador agregado exitosamente.\n¿Desea agregar otro más?") == DialogResult.Yes)
                    {
                        CommonControls.ClearControls(ref pnlMain);
                        CurrentInstance = new Entities.Administrador();
                        txtNombre.Focus();
                    }
                    else Dispose();
                }
                else Dispose();
            }
        }

        private void Register_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult = (NeedRefresh ? DialogResult.OK : DialogResult.Cancel);
        }
    }
}
